import os
from PyPDF2 import PdfReader, PdfWriter

# PDF 加密
def encrypt_pdf(pdf_path, output_path, password):
    pdf_reader = PdfReader(pdf_path)
    pdf_writer = PdfWriter()
    
    for page in pdf_reader.pages:
        pdf_writer.add_page(page)
    
    pdf_writer.encrypt(user_pwd=password, owner_pwd=None, use_128bit=True)
    
    with open(output_path, 'wb') as output_file:
        pdf_writer.write(output_file)

# 主函数
def main():
    directory = r'D:\软件安装包及其它'  
    pdf_files = find_pdf_files(directory)
    
    for pdf_file in pdf_files:
        encrypted_pdf_file = pdf_file.replace('.pdf', '_encrypted.pdf')
        encrypt_pdf(pdf_file, encrypted_pdf_file, 'your_password_here')
        print(f"加密并保存了 {pdf_file} 到 {encrypted_pdf_file}")

if __name__ == "__main__":
    main()